<?php

namespace app\admin\controller;

use app\admin\model\system\SystemMenus;
use app\admin\model\system\SystemRole;
use app\admin\model\user\User as UserModel;
use app\common\model\system\SystemConfig;
use think\facade\Config;


/**
 * @title Index 控制器
 * Class Index
 * @package app\admin\controller
 */
class Index extends AuthController
{
    /**
     * @title 功能首页
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 获取管理员的信息
        $adminInfo = $this->adminInfo->toArray();
        $roles = explode(",", $adminInfo["roles"]);
        $site_logo = SystemConfig::getOneConfig("menu_name", "site_logo")->toArray();

        $this->assign([
            'menuList' => SystemMenus::menuList(),
            'site_logo' => json_decode($site_logo['value'], true),
            'new_order_audio_link' => sys_config('new_order_audio_link'),
            'role_name' => SystemRole::where('id', $roles[0])->field('role_name')->find(),
            'workermanPort' => Config::get('workerman.admin.port')

        ]);
        return $this->fetch();
    }

    /**
     * @title 主要的页面
     */
    public function main()
    {
        /*首页第一行统计*/
        $now_month = strtotime(date('Y-m'));//本月
        $pre_month = strtotime(date('Y-m', strtotime('-1 month')));//上月
        $now_day = strtotime(date('Y-m-d'));//今日
        $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨天时间戳
        $beforyester_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天时间戳
        $topData = [
            "orderDeliveryNum" => 66,
            "orderRefundNum" => 88,
            "stockProduct" => 99,
            "treatedExtract" => 11,
        ];
        $first_line = [
            "d_num" => [
                "data" => 1,
                "percent" => 2,
                "is_plus" => 100,
            ],
            "d_price" => [
                "data" => 2,
                "percent" => 3,
                "is_plus" => 2,
            ],
            "day" => [
                "data" => 3,
                "percent" => 4,
                "is_plus" => 1
            ],
            "month" => [
                "data" => 3,
                "percent" => 4,
                "is_plus" => 1
            ]


        ];
        $this->assign([
            "topData" => $topData,
            "first_line" => $first_line
        ]);
        return $this->fetch();

    }

    /**
     * 用户图表
     */
    public function userchart()
    {
        header('Content-type:text/json');

        $starday = date('Y-m-d', strtotime('-30 day'));
        $yesterday = date('Y-m-d');

        $user_list = UserModel::where('add_time', 'between time', [$starday, $yesterday])
            ->field("FROM_UNIXTIME(add_time,'%m-%e') as day,count(*) as count")
            ->group("FROM_UNIXTIME(add_time, '%Y%m%e')")
            ->order('add_time asc')
            ->select()->toArray();
        $chartdata = [];
        $data = [];
        $chartdata['legend'] = ['用户数'];//分类
        $chartdata['yAxis']['maxnum'] = 0;//最大值数量
        $chartdata['xAxis'] = [date('m-d')];//X轴值
        $chartdata['series'] = [0];//分类1值
        if (!empty($user_list)) {
            foreach ($user_list as $k => $v) {
                $data['day'][] = $v['day'];
                $data['count'][] = $v['count'];
                if ($chartdata['yAxis']['maxnum'] < $v['count'])
                    $chartdata['yAxis']['maxnum'] = $v['count'];
            }
            $chartdata['xAxis'] = $data['day'];//X轴值
            $chartdata['series'] = $data['count'];//分类1值
        }
        return app('json')->success('ok', $chartdata);
    }


}